package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.base.model.PageParams;
import com.xuecheng.base.model.PageResult;
import com.xuecheng.content.mapper.CourseBaseMapper;
import com.xuecheng.content.mapper.CourseMarketMapper;
import com.xuecheng.content.model.dto.AddCourseDto;
import com.xuecheng.content.model.dto.QueryCourseParamsDto;
import com.xuecheng.content.model.dto.UpdateCourseDto;
import com.xuecheng.content.model.po.CourseBase;
import com.xuecheng.content.model.po.CourseMarket;
import com.xuecheng.content.model.vo.CourseVO;
import com.xuecheng.content.service.CourseBaseInfoService;
import com.xuecheng.content.service.CourseMarketService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class CourseBaseInfoServiceImpl implements CourseBaseInfoService {

  private final CourseBaseMapper courseBaseMapper;
  private final CourseMarketService courseMarketService;


  @Override
  public PageResult<CourseBase> list(PageParams page, QueryCourseParamsDto dto) {
    //拼装查询条件
    LambdaQueryWrapper<CourseBase> queryWrapper = new LambdaQueryWrapper<>();
    //根据名称模糊查询,在sql中拼接 course_base.name like '%值%'
    queryWrapper.like(StringUtils.hasText(dto.getCourseName()), CourseBase::getName, dto.getCourseName());
    //根据课程审核状态查询 course_base.audit_status = ?
    queryWrapper.eq(StringUtils.hasText(dto.getAuditStatus()), CourseBase::getAuditStatus, dto.getAuditStatus());
    //按课程发布状态查询
    queryWrapper.eq(StringUtils.hasText(dto.getPublishStatus()), CourseBase::getStatus, dto.getPublishStatus());
    //分页参数对象
    //创建page分页参数对象，参数：当前页码，每页记录数
    //开始进行分页查询
    Page<CourseBase> pageResult = courseBaseMapper.selectPage(new Page<>(page.getPageNo(), page.getPageSize()), queryWrapper);
    //数据列表
    List<CourseBase> items = pageResult.getRecords();
    //总记录数
    long total = pageResult.getTotal();

    //List<T> items, long counts, long page, long pageSize
    return new PageResult<>(items, total, page.getPageNo(), page.getPageSize());
  }

  @Override
  @Transactional
  public CourseVO add(Long companyId, AddCourseDto dto) {
    // 参数的合法性校验
    if (!StringUtils.hasText(dto.getName())) {
      XueChengPlusException.cast("课程名称为空");
    }

    if (!StringUtils.hasText(dto.getMt())) {
      throw new RuntimeException("课程分类为空");
    }

    if (!StringUtils.hasText(dto.getSt())) {
      throw new RuntimeException("课程分类为空");
    }

    if (!StringUtils.hasText(dto.getGrade())) {
      throw new RuntimeException("课程等级为空");
    }

    if (!StringUtils.hasText(dto.getTeachmode())) {
      throw new RuntimeException("教育模式为空");
    }


    // 添加课程信息
    CourseBase courseBasePO = new CourseBase();
    BeanUtils.copyProperties(dto, courseBasePO);
    courseBasePO.setCompanyId(companyId);
    courseBasePO.setCreateDate(LocalDateTime.now());
    //审核状态默认为未提交
    courseBasePO.setAuditStatus("202002");
    //发布状态为未发布
    courseBasePO.setStatus("203001");
    //插入数据库
    int insert = courseBaseMapper.insert(courseBasePO);
    if (insert <= 0) {
      throw new RuntimeException("添加课程失败");
    }

    // 添加课程营销信息
    CourseMarket courseMarketPO = new CourseMarket();
    BeanUtils.copyProperties(dto, courseMarketPO);
    //课程的id
    Long courseId = courseBasePO.getId();
    courseMarketPO.setId(courseId);

    courseMarketService.save2(courseMarketPO);


    return getOne(courseId);
  }

  public CourseVO getOne(Long courseId) {
    CourseBase courseBase = courseBaseMapper.selectById(courseId);
    if (courseBase == null) {
      return null;
    }
    CourseVO courseVO = new CourseVO();
    BeanUtils.copyProperties(courseBase, courseVO);

    // 课程营销
    CourseMarket courseMarket = courseMarketService.getById(courseId);
    if (courseMarket != null) {
      BeanUtils.copyProperties(courseMarket, courseVO);
    }
    return courseVO;
  }

  @Override
  public CourseVO update(Long companyId, UpdateCourseDto dto) {
    //拿到课程id
    Long courseId = dto.getId();
    //查询课程信息
    CourseBase courseBase = courseBaseMapper.selectById(courseId);
    if(courseBase == null){
      XueChengPlusException.cast("课程不存在");
    }

    //数据合法性校验
    //根据具体的业务逻辑去校验
    //本机构只能修改本机构的课程
    if(!companyId.equals(courseBase.getCompanyId())){
      XueChengPlusException.cast("本机构只能修改本机构的课程");
    }

    //封装数据
    BeanUtils.copyProperties(dto, courseBase);
    //修改时间
    courseBase.setChangeDate(LocalDateTime.now());
    int i = courseBaseMapper.updateById(courseBase);
    if(i <= 0) {
      XueChengPlusException.cast("课程修改失败");
    }

    //修改营销信息
    CourseMarket courseMarket = new CourseMarket();
    BeanUtils.copyProperties(dto, courseMarket);
    courseMarketService.save2(courseMarket);
    return getOne(courseId);
  }
}
